package main.java.view;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import main.java.model.Doors;

public class DoorsView extends JPanel implements Observer {
    
    //List of positions
    private static final String [] POSITIONS = {"Closed", "Opened", "Maneuvering", "Failure"};
    
    //static final variables to change positions id easier
    private static final int CLOSED_ID = 0;
    private static final int MANEUVERING_ID = 1;
    private static final int OPENED_ID = 2;
    private static final int FAILURE_ID = 3;
    
    //static final variables of view dimension to modify them easier
    private static final int SIZE1 = 200;
    
    //Path of each image of the door
    private static String doorsClosedImage = "images/DoorsClosed.png";
    private static String doorsManeuveringImage = "images/DoorsManeuvering.png";
    private static String doorsOpenedImage = "images/DoorsOpened.png";
    private static String doorsFailureImage = "images/DoorsFailure.png";
    
    private Image doorsImage;

    /**
     * Constructor DoorsView
     */
    public DoorsView() {
        //Set to Closed door
        this.doorsImage = new ImageIcon(doorsClosedImage).getImage();
        
        //Initialize panel info
        Dimension size = new Dimension(doorsImage.getWidth(null), doorsImage.getHeight(null));
        this.setPreferredSize(size);
        this.setMinimumSize(size);
        this.setMaximumSize(size);
        this.setSize(size);
        this.setLayout(null);
    }
    
    /**
     * Switch the image depending on the position of the door
     * @param position : position of the door
     */
    public void switchImage(String position) {
        switch (position) {
            //If closed then set doorsClosedImage
            case "Closed": 
                this.doorsImage = new ImageIcon(doorsClosedImage).getImage();
                break;
            //If opened then set doorsOpenedImage
            case "Opened": 
                this.doorsImage = new ImageIcon(doorsOpenedImage).getImage();
                break;
            //If maneuvering then set doorsManeuveringImage
            case "Maneuvering": 
                this.doorsImage = new ImageIcon(doorsManeuveringImage).getImage();                
                break;
            //If failure then set doorsFailureImage
            case "Failure": 
                this.doorsImage = new ImageIcon(doorsFailureImage).getImage();
                break;
            default:
                break;
        }
        //repaint
        paintImmediately(0, 0, SIZE1, SIZE1); 
    }

    /**
     * Draw image
     */
    public void paintComponent(Graphics g) {
        g.drawImage(doorsImage, 0, 0, null);
    }

    /**
     * Update image 
     * Depending on the position of the doors
     * @param Observable : what type of instance
     * @param Object : given change 
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof Doors) {
            switch (arg.toString()) 
            {
                case "Closed":
                    switchImage(POSITIONS[CLOSED_ID]);
                    break;
                case "Opened":
                    switchImage(POSITIONS[MANEUVERING_ID]);
                    break;
                case "Maneuvering":
                    switchImage(POSITIONS[OPENED_ID]);                    
                    break;
                case "Failure":
                    switchImage(POSITIONS[FAILURE_ID]);
                    break;
                default:
                    break;
            }
        }
    }
}
